package com.xuanxuan.socketdemo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xuanxuan.socketdemo.R;
import com.xuanxuan.socketdemo.adapter.BluetoothDeviceAdapter;
import com.ywicc.commlibs.bluetooth.BluetoothUtils;

import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class BluetoothClientActivity extends AppCompatActivity {

    private final static String TAG = "BluetoothClientActivity";

    private RecyclerView recyclerView;
    private Button btn_client_stop;
    private BluetoothUtils utils;
    private BluetoothClientHandler handler;
    private String strTmpData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth_client_activity);

        handler = new BluetoothClientHandler(this, this.getMainLooper());

        recyclerView = findViewById(R.id.rv_bluetooth_client);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        btn_client_stop = findViewById(R.id.btn_bluetooth_client_stop);

        utils = new BluetoothUtils();
        try {
            utils.initBluetooth();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void btn_bluetooth_client(View view) {
        Set<BluetoothDevice> listDevice = utils.getPairedDevice();

        List<BluetoothDevice> listDevices = new ArrayList<BluetoothDevice>();
        List<String> listName = new ArrayList<String>();
        List<String> listMac = new ArrayList<String>();

        for (BluetoothDevice device : listDevice) {
            listName.add(device.getName());
            listMac.add(device.getAddress());
            listDevices.add(device);
        }

        BluetoothDeviceAdapter adapter = new BluetoothDeviceAdapter(listDevices, listName, listMac);
        adapter.setOnBluetoothDeviceAdapterClickListener(new BluetoothDeviceAdapter.OnBluetoothDeviceAdapterClickListener() {
            @Override
            public void onClick(BluetoothDevice device) {
                Toast.makeText(BluetoothClientActivity.this, "点击了: " + device.getName(), Toast.LENGTH_SHORT).show();

                utils.onStartClient(device, new BluetoothUtils.BluetoothUtilsClientImpl() {
                    @Override
                    public void callBackClientData(byte[] data) {
                        Log.w(TAG, new String(data, StandardCharsets.UTF_8));

                        Message message = new Message();
                        message.what = 1;
                        strTmpData = new String(data, StandardCharsets.UTF_8);
                        handler.sendMessage(message);
                    }
                });
            }
        });
        recyclerView.setAdapter(adapter);
    }

    public void btn_bluetooth_client_stop(View view) {
        utils.stopClient();
    }

    public void btn_bluetooth_client_send(View view) {
        utils.onSendData("客户端发送数据".getBytes(StandardCharsets.UTF_8), BluetoothUtils.BLUETOOTH_CLIENT);
        Toast.makeText(this, "发送数据", Toast.LENGTH_SHORT).show();
    }

    public static class BluetoothClientHandler extends Handler {
        private final WeakReference<BluetoothClientActivity> weakActivity;

        public BluetoothClientHandler(BluetoothClientActivity activity, Looper looper) {
            super(looper);
            weakActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            Toast.makeText(weakActivity.get(), "接收数据: " + weakActivity.get().strTmpData, Toast.LENGTH_SHORT).show();
        }
    }
}